from urllib.parse import quote

import execjs
import requests


def load_data(city):
    with open("parse.js", "r", encoding="utf8") as f:
        content = f.read()
        js_code = execjs.compile(content)
        result = js_code.call("get_sign")

    url = 'https://index.mysteel.com/api/pricetrend/getChartMultiCity.htm'
    params = {
        "catalog": "%E8%A7%92%E9%92%A2_:_%E8%A7%92%E9%92%A2",
        "city": f"{quote(city)}",
        "spec": "Q235B%2050*50*5_:_Q235B_50*50*5",
        "startTime": "2025-04-01",
        "endTime": "2025-05-08",
        "callback": "json",
        "v": result[1],
        "Api-Token": result[0],
        "Timestamp": result[1]
    }
    # 发送GET请求
    response = requests.get(url, params=params)
    print(response.text)
    data = js_code.call("decryptEncryptFields", response.json(), "data", True)
    print(data)
    return data

if __name__ == '__main__':
    load_data("北京")
